﻿//-----------------------------------------------------------------------
// <copyright file="DbContextTypeResolver.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: DbContextTypeResolver.cs
// * history : created by qinchaoyue 2017-12-14 07:10:36
// </copyright>
//-----------------------------------------------------------------------

namespace Slwy.Data.Entity
{
    using Microsoft.Extensions.DependencyInjection;

    using Slwy.Data.Entity.Interfaces;
    using Slwy.FrameCore.Dependency;
    using Slwy.Logger.Contracts;
    using Slwy.MySql.Contracts;

    public class DbContextTypeResolver : IDbContextTypeResolver
    {
        private readonly IMySqlConnection _mySqlConnection;

        private readonly ILogger _logger;

        public DbContextTypeResolver(IMySqlConnection mySqlConnection, ILogger logger)
        {
            this._mySqlConnection = mySqlConnection;
            this._logger = logger;
        }

        public DbContextTypeResolver(IMySqlConnection mySqlConnection)
        {
            this._mySqlConnection = mySqlConnection;
            this._logger = ServiceLocator.Instance.GetService<ILogger>();
        }

        public DbContextTypeResolver()
        {
            this._mySqlConnection = ServiceLocator.Instance.GetService<IMySqlConnection>();
            this._logger = ServiceLocator.Instance.GetService<ILogger>();
        }

        public IUnitOfWork Resolve<TEntity, TKey>()
            where TEntity : IEntityBase<TKey>
        {
            return new DbContextBase(this._mySqlConnection, this._logger);
        }
    }
}
